/*
 * Copyright 2012 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.chacha.cmdproxy;

import java.io.IOException;

import org.chacha.client.command.account.AddAccountRequest;
import org.chacha.client.command.account.DelAccountRequest;
import org.chacha.client.command.account.ModifyAccountRequest;
import org.chacha.client.command.account.QueryAccountRequest;
import org.chacha.client.command.account.QueryAccountResponse;
import org.chacha.context.AccountManageContext;
import org.chacha.context.AccountManageContextImpl;
import org.chacha.context.LoginContext;
import org.chacha.context.LoginContextImpl;
import org.chacha.entity.AccountRequest;
import org.chacha.entity.AuthRequest;
import org.chacha.entity.AuthResponse;
import org.chacha.entity.Request;
import org.chacha.entity.Response;
import org.chacha.entity.ResponseCode;
import org.chacha.log.LogPrn;
import org.chacha.util.JsonPojoMapper;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 
 *	@author: hejing
 *	@date: 2012-11-26-上午11:09:16
 */
public class CommandHandler {
    private static LogPrn log = new LogPrn(CommandHandler.class);
    
	@SuppressWarnings("finally")
	public static String handle(String reqJson) throws JsonMappingException, JsonGenerationException, IOException{
			Request req=(Request)JsonPojoMapper.fromJson(reqJson,Request.class);
			String opt=req.getOpt();
			if(AuthRequest.CMD_AUTH.equalsIgnoreCase(opt)){	//密码认证命令
				return handleLogin(reqJson);
			}else if(AuthRequest.CMD_TOKEN.equalsIgnoreCase(opt)){//令牌认证
				return handleLoginToken(reqJson);
			}else if(AuthRequest.CMD_LOGOUT.equalsIgnoreCase(opt)){//令牌注销
				return handleLogout(reqJson);
			}else if(AccountRequest.ADD.equalsIgnoreCase(opt)){//帐号增加
				return handleAddAccount(reqJson);
			}else if(AccountRequest.MODIFY.equalsIgnoreCase(opt)){//帐号修改
				return handleModifyAccount(reqJson);
			}else if(AccountRequest.DEL.equalsIgnoreCase(opt)){//帐号删除
				return handleDelAccount(reqJson);
			}else if(AccountRequest.QUERY.equalsIgnoreCase(opt)){//帐号查询
				return handleQueryAccount(reqJson);
			}else{	//非法命令
				return handleInvalidCommand();
			}
	}

	private static String handleQueryAccount(String reqJson) throws JsonMappingException, JsonGenerationException, IOException {
		QueryAccountResponse response=new QueryAccountResponse();
		try{
			AccountManageContext am=new AccountManageContextImpl();
			QueryAccountRequest queryReq=(QueryAccountRequest)JsonPojoMapper.fromJson(reqJson,QueryAccountRequest.class);
			response=am.query(queryReq);
		}catch(Exception e){//服务器异常返回
			response.setResult(ResponseCode.EXCEPTION);
			log.error("CommandHandler exception:",e);
		}finally{
			return JsonPojoMapper.toJson(response, true);
		}
	}

	private static String handleDelAccount(String reqJson) throws JsonMappingException, JsonGenerationException, IOException {
		Response response=new Response();
		try{
			AccountManageContext am=new AccountManageContextImpl();
			DelAccountRequest delReq=(DelAccountRequest)JsonPojoMapper.fromJson(reqJson,DelAccountRequest.class);
			response=am.delete(delReq);
		}catch(Exception e){//服务器异常返回
			response.setResult(ResponseCode.EXCEPTION);
			log.error("CommandHandler exception:",e);
		}finally{
			return JsonPojoMapper.toJson(response, true);
		}
	}

	/**
	 * 非法命令
	 * @param reqJson
	 * @return
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 * @throws IOException
	 */
	public static String handleInvalidCommand() throws JsonMappingException, JsonGenerationException, IOException{
		Response response=new Response();
		response.setResult(ResponseCode.ERROR_COMMAND);
		return JsonPojoMapper.toJson(response, true);
	}
	/**
	 * 密码认证
	 * @param reqJson
	 * @return
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 * @throws IOException
	 */
	public static String handleLogin(String reqJson) throws JsonMappingException, JsonGenerationException, IOException{
		AuthResponse response=new AuthResponse();
		try{
			LoginContext login=new LoginContextImpl();
			AuthRequest authReq=(AuthRequest)JsonPojoMapper.fromJson(reqJson,AuthRequest.class);
			response=login.login(authReq);
		}catch(Exception e){//服务器异常返回
			response.setResult(ResponseCode.EXCEPTION);
			log.error("CommandHandler exception:",e);
		}finally{
			return JsonPojoMapper.toJson(response, true);
		}
	}
	/**
	 * 令牌认证
	 * @param reqJson
	 * @return
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 * @throws IOException
	 */
	public static String handleLoginToken(String reqJson) throws JsonMappingException, JsonGenerationException, IOException{
		AuthResponse response=new AuthResponse();
		try{
			LoginContext login=new LoginContextImpl();
			AuthRequest authReq=(AuthRequest)JsonPojoMapper.fromJson(reqJson,AuthRequest.class);
			response=login.loginToken(authReq);
		}catch(Exception e){//服务器异常返回
			response.setResult(ResponseCode.EXCEPTION);
			log.error("CommandHandler exception:",e);
		}finally{
			return JsonPojoMapper.toJson(response, true);
		}
	}
	/**
	 * 令牌注销
	 * @param reqJson
	 * @return
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 * @throws IOException
	 */
	public static String handleLogout(String reqJson) throws JsonMappingException, JsonGenerationException, IOException{
		AuthResponse response=new AuthResponse();
		try{
			LoginContext login=new LoginContextImpl();
			AuthRequest authReq=(AuthRequest)JsonPojoMapper.fromJson(reqJson,AuthRequest.class);
			response=login.logout(authReq);
		}catch(Exception e){//服务器异常返回
			response.setResult(ResponseCode.EXCEPTION);
			log.error("CommandHandler exception:",e);
		}finally{
			return JsonPojoMapper.toJson(response, true);
		}
	}
	/**
	 * 帐号增加
	 * @param reqJson
	 * @return
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 * @throws IOException
	 */
	public static String handleAddAccount(String reqJson) throws JsonMappingException, JsonGenerationException, IOException{
		Response response=new Response();
		try{
			AccountManageContext am=new AccountManageContextImpl();
			AddAccountRequest addReq=(AddAccountRequest)JsonPojoMapper.fromJson(reqJson,AddAccountRequest.class);
			response=am.add(addReq);
		}catch(Exception e){//服务器异常返回
			response.setResult(ResponseCode.EXCEPTION);
			log.error("CommandHandler exception:",e);
		}finally{
			return JsonPojoMapper.toJson(response, true);
		}
	}
	/**
	 * 帐号修改
	 * @param reqJson
	 * @return
	 * @throws IOException 
	 * @throws JsonGenerationException 
	 * @throws JsonMappingException 
	 */
	private static String handleModifyAccount(String reqJson) throws JsonMappingException, JsonGenerationException, IOException {
		Response response=new Response();
		try{
			AccountManageContext am=new AccountManageContextImpl();
			ModifyAccountRequest modifyReq=(ModifyAccountRequest)JsonPojoMapper.fromJson(reqJson,ModifyAccountRequest.class);
			response=am.update(modifyReq);
		}catch(Exception e){//服务器异常返回
			response.setResult(ResponseCode.EXCEPTION);
			log.error("CommandHandler exception:",e);
		}finally{
			return JsonPojoMapper.toJson(response, true);
		}
	}
}
